package org.aeon.heroicsagatextedition.round.product;

import org.aeon.heroicsagatextedition.constants.CommonlyConstants;
import org.aeon.heroicsagatextedition.game.GameSettlement;
import org.aeon.heroicsagatextedition.output.OutputFactory;
import org.aeon.heroicsagatextedition.role.product.RoleUnit;

import java.util.Deque;
import java.util.LinkedList;
import java.util.stream.Collectors;

/**
 * @projectName: HeroicSagaTextEdition
 * @package: org.aeon.heroicsagatextedition.round
 * @className: Round
 * @author: MissingAeon
 * @date: 2023/12/22 15:12
 * @description: 回合抽象类
 */
public abstract class Round {
    protected static Deque<RoleUnit> roles;         // 所有玩家列表
    protected static int participantCount = 0;      // 玩家回合数
    private static int recordCount = 0;             // 当前回合数
    protected Round successor;                      // 继承者
    protected RoleUnit role;                        // 角色

    public Round(RoleUnit role) {
        participantCount++;
        this.role = role;
    }

    public int getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(int recordCount) {
        Round.recordCount = recordCount;
    }

    public void showModifyRecordCount(int recordCount) {
        setRecordCount(recordCount);
        System.out.println("******************当前回合数" + recordCount + "***********************");
    }

    public void setSuccessor(Round successor) {
        this.successor = successor;
    }

    public void setRoles(Deque<RoleUnit> roles) {
        Round.roles = roles;
    }

    public void roundResponse() {
        // next();
    }

    public void matchingRound() {
        if (!GameSettlement.isState()) {
            return;
        }
        if (role.isSurvival()) {
            roundResponse();
        }
        next();
    }

    public void next() {
        if (successor != null)
            successor.matchingRound();
        else {
            if (campPlayers(CommonlyConstants.EVIL).size() == 0) {
                new GameSettlement(true);
            }
        }
    }

    /**
     * 获取不同阵营的非死亡单位
     *
     * @param camp
     * @return
     */
    public Deque<RoleUnit> campPlayers(int camp) {
        return roles.stream().filter(role -> role.getCamp() == camp && role.isSurvival()).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * 展示敌方阵营的单位
     *
     * @param camp
     * @return
     */
    public Deque<RoleUnit> displayCampPlayers(int camp) {
        System.out.println("当前敌方阵营目标：");
        int number = 1;
        Deque<RoleUnit> campPlayers = campPlayers(camp);
        StringBuilder sb = new StringBuilder();
        sb.append("--");
        for (RoleUnit role : campPlayers) {
            sb.append(number).append(".").append(role.getName()).append("  ");
            if (number % 4 == 0) {
                sb.append("\n").append("--");
            }
            number++;
        }
        OutputFactory.create().print( sb.toString());
        return campPlayers;
    }
}
